﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.APIs.Cards;
using System.Threading;

namespace Waker.Cards
{
    public partial class FormCardSetting : Base.FormBasePop
    {
        public FormCardSetting()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            var a = CardHelper.RC500USB_init();//打开USB
            byte type = 0;
            UInt32 snr = 0;
            byte size = 0;
            byte[] szpwd = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
            byte[] data = new byte[16];
            for (int i = 0; i < 16; i++)
            {
                data[i] = Convert.ToByte(i);
            }
            int value = 0x500;
            //CardHelper.RC500USB_config(); 
            //CardHelper.RC500USB_reset(2);
            for (int i = 0; i < 10; i++)
            {
                var b = CardHelper.RC500USB_request(0, out type);
                if (b == 0)//对第一个读卡器进行操作
                {
                    break;
                }
            }
            if (CardHelper.RC500USB_anticoll(0, out snr) != 0)//进行防碰撞选择成功则返回卡号
                return;
            if (CardHelper.RC500USB_select(snr, out size) != 0)//选择卡
                return;
            if (CardHelper.RC500USB_authkey(0, szpwd, 0) != 0)//验证第0 扇区的密钥A
                return;
            if (CardHelper.RC500USB_write(2, data) != 0)//往第二块写入数据
                return;
            if (CardHelper.RC500USB_read(2, data) != 0) //读取第二块的数据
                return;
            //此处可以使用data中的数据
            if (CardHelper.RC500USB_writeval(1, value) != 0)//往第一块写入数值value
                return;
            //byte trans_addr = 1;
            //if (CardHelper.RC500USB_value(1, 0xc1, value, trans_addr) != 0)//把第一块的数值加上value
            //    return;
            if (CardHelper.RC500USB_readval(1, out value) != 0)//读取第一块的数值
                return;
            CardHelper.RC500USB_halt();
            CardHelper.RC500USB_buzzer(1, 25, 25, 1);
            CardHelper.RC500USB_exit();
            System.Threading.Thread.Sleep(100);
        }
    }
}
